//
//  UIViewController+WKExtension.m
//  Wanka
//
//  Created by 郭晓东 on 2017/10/3.
//  Copyright © 2017年 郭晓东. All rights reserved.
//

#import "UIViewController+WKExtension.h"
#import <objc/runtime.h>

static NSString *errorViewKey = @"errorViewKey";

@implementation UIViewController (WKExtension)

- (void)back
{
    [self.view endEditing:YES];
    ///push 栈里面只有当前一个控制器
    if (self.navigationController.viewControllers.count == 1) {
        [self.navigationController dismissViewControllerAnimated:YES completion:^{
        }];
    }
    else
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}



// 适配错误页内容
- (void)configureMaxY:(CGFloat)maxY netErrorView:(NSString *)imgName title:(NSString *)title btnTitle:(NSString *)btnTitle
{
    CGFloat kScale = (CGFloat)SCREEN_W / 375;
    maxY = maxY *kScale;
    UIImageView *imageView;
    UILabel *label;
    UIButton *button;
    if (!self.wkErrorView) {
        self.wkErrorView = [[UIView alloc] initWithFrame:(CGRect){0,0,self.view.bounds.size}];
        self.wkErrorView.backgroundColor = UIColorFromHex(0xF5F5F5);
        [self.view addSubview:self.wkErrorView];
        
        
        imageView = [[UIImageView alloc] init];
        [self.wkErrorView addSubview:imageView];
        
        label = [[UILabel alloc] init];
        label.backgroundColor = [UIColor clearColor];
        label.textColor = UIColorFromHex(0xa2a2a2);
        label.font = fontWithPingfang(14);
        label.textAlignment = NSTextAlignmentCenter;
        [self.wkErrorView addSubview:label];
        
        button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.font = fontWithPingfang(16);
        button.layer.borderColor = UIColorFromHex(0xff6600).CGColor;
        button.layer.borderWidth = 1.f;
        button.layer.cornerRadius = 3.f;
        button.layer.masksToBounds = YES;
        button.backgroundColor = UIColorFromHex(0xf5f5f5);
        [button setTitleColor:UIColorFromHex(0xff6600) forState:UIControlStateNormal];
        [self.wkErrorView addSubview:button];
    }
    self.wkErrorView.hidden = YES;
    
    UIImage *image = [UIImage imageNamed:imgName];
    if (!image) {
        image = [UIImage imageNamed:@"error_404"];
    }
    CGSize size = self.view.frame.size;

    
    imageView.frame = (CGRect){(size.width - image.size.width) *0.5f , maxY , image.size};
    imageView.image = image;
    
    label.text = title;
    label.frame = CGRectMake(0, CGRectGetMaxY(imageView.frame) + 20, size.width, 20);
    
    [button setTitle:btnTitle forState:UIControlStateNormal];
    button.frame = CGRectMake((size.width - 140) *0.5f , CGRectGetMaxY(label.frame) + 32, 140, 38);
    [button addTarget:self action:@selector(clickBaseError:) forControlEvents:UIControlEventTouchUpInside];
}

-(void)clickBaseError:(id)send
{
    NSLog(@"clickBaseError 方法没有实现");
}

// 显示错误页
- (void)isShowNetErrorView:(BOOL)isHidden
{
    self.wkErrorView.hidden = !isHidden;
}

-(void)setWkErrorView:(UIView *)wkErrorView
{
    objc_setAssociatedObject(self, &errorViewKey, wkErrorView, OBJC_ASSOCIATION_RETAIN);
}
-(UIView *)wkErrorView
{
    return objc_getAssociatedObject(self, &errorViewKey);
}

@end
